% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Warranty Disclaimer and Copyright Notice
% 
% Copyright (C) 2003-2010 Institute for Systems Biology, Seattle, Washington, USA.
% 
% The Institute for Systems Biology and the authors make no representation about the suitability or accuracy of this software for any purpose, and makes no warranties, either express or implied, including merchantability and fitness for a particular purpose or that the use of this software will not infringe any third party patents, copyrights, trademarks, or other rights. The software is provided "as is". The Institute for Systems Biology and the authors disclaim any liability stemming from the use of this software. This software is provided to enhance knowledge and encourage progress in the scientific community. 
% 
% This is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
% 
% You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [DATA,s,val1,val2,val3] = FC_preprocess(DATA);

%Note that this function assumes that the range of scatter and fluoresence
%intensity values is between 0 and 1023.  When FACS machines with other
%precision ranges are used, these values should be changed.

N = length(DATA);
for n = 1:N;
    
    DATA{n} = double(DATA{n});
    [s(n),three] = size(DATA{n});
    
%         hold off;plot(DATA{n}(:,1),DATA{n}(:,2),'b.','MarkerSize',6);hold on
    
    %1 The first second and last 0.2 seconds of data were removed to minimize errors due
    %  to unstable sample flow through the cytometer
    p = 0.002;
    pos = [1:ceil(s(n)*p) s(n)-floor(s(n)*p):s(n)];
    DATA{n}(pos,:) = [];
    
    %2 All FSC and SSC data with minimal or maximal values (i.e., 0 and 2^10-1) were
    %  removed, as these are undefined
    minval = 0;
    maxval = 1023;
    pos = find(DATA{n}(:,1)<=minval|DATA{n}(:,2)<=minval|DATA{n}(:,1)>=maxval|DATA{n}(:,2)>=maxval|DATA{n}(:,3)<=minval|DATA{n}(:,3)>=maxval);
    DATA{n}(pos,:) = [];
    
    %3 The bottom and top 5% of the FSC and SSC data were excluded to limit the
    %  influence of cellular debris and aggregated cells
    prc = 5;
    val1(n,:) = prctile(DATA{n}(:,1),[prc 100-prc]);
    val2(n,:) = prctile(DATA{n}(:,2),[prc 100-prc]);
    val3(n,:) = prctile(DATA{n}(:,3),[prc 100-prc]);
    pos = find(DATA{n}(:,1)<=val1(n,1)|DATA{n}(:,2)<=val2(n,1)|DATA{n}(:,1)>=val1(n,2)|DATA{n}(:,2)>=val2(n,2));
    DATA{n}(pos,:) = [];
    
    [s(n),three] = size(DATA{n});
    
%         plot(DATA{n}(:,1),DATA{n}(:,2),'r.','MarkerSize',6);grid;title(num2str(n));pause

end
val1 = [min(val1(:,1)) max(val1(:,2))];
val2 = [min(val2(:,1)) max(val2(:,2))];
val3 = [min(val3(:,1)) max(val3(:,2))];
